package com.jzxiang.pickerview.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.config.DefaultConfig;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static java.lang.System.currentTimeMillis;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, OnDateSetListener {
    TimePickerDialog mDialogAll;
    TimePickerDialog mDialogYearMonth;
    TimePickerDialog mDialogYearMonthDay;
    TimePickerDialog mDialogMonthDayHourMinute;
    TimePickerDialog mDialogHourMinute;
    TextView mTvTime;

    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private long millseconds1 = 1527654600000L;

    private int year, monthOfYear, dayOfMonth, hourOfDay, minute;
    private int Interval = 10;
    private long CurrentTimeMillis;

    private void getCurrentTimeMillis() {
        CurrentTimeMillis = currentTimeMillis();
//        currentTimeMillis = 1527649200000L;

        //TODO 获取时间数据
        Calendar calendar = Calendar.getInstance();
//        minute = calendar.get(Calendar.MINUTE);
        minute = 59;
        long minute1 = 1000 * 60 * (Interval + 1);
        int result = (59 - minute + 1) / Interval;
        if (result % Interval == 0) {
            CurrentTimeMillis = CurrentTimeMillis - minute1;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getCurrentTimeMillis();

//        long tenYears = 2L * 1000 * 60 * 60 * 24L;
//        mDialogAll = new TimePickerDialog.Builder()
//                .setCallBack(this)
//                .setCancelStringId(DefaultConfig.CANCEL)
//                .setSureStringId(DefaultConfig.SURE)
//                .setTitleStringId(DefaultConfig.TITLE)
//                .setYearText(DefaultConfig.YEAR)
//                .setMonthText(DefaultConfig.MONTH)
//                .setDayText(DefaultConfig.DAY)
//                .setHourText(DefaultConfig.HOUR)
//                .setMinuteText(DefaultConfig.MINUTE)
//                .setCyclic(false)
//                .setMinMillseconds(CurrentTimeMillis)
//                .setMaxMillseconds(CurrentTimeMillis + tenYears)
//                .setCurrentMillseconds(millseconds1)
//                .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
//                .setType(Type.ALL)
//                .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
//                .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
//                .setWheelItemTextSize(12)
//                .setIsIntervalFiveMinute(true, 30)
//                .build();

//        mDialogAll = new TimePickerDialog.Builder()
//                .setMinMillseconds(System.currentTimeMillis())
//                .setThemeColor(R.color.colorPrimary)
//                .setWheelItemTextSize(16)
//                .setCallBack(this)
//                .build();
        mDialogYearMonth = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH)
                .setThemeColor(getResources().getColor(R.color.colorPrimary))
                .setCallBack(this)
                .build();
        mDialogYearMonthDay = new TimePickerDialog.Builder()
                .setType(Type.YEAR_MONTH_DAY)
                .setCallBack(this)
                .build();
        mDialogMonthDayHourMinute = new TimePickerDialog.Builder()
                .setType(Type.MONTH_DAY_HOUR_MIN)
                .setCallBack(this)
                .build();
        mDialogHourMinute = new TimePickerDialog.Builder()
                .setType(Type.HOURS_MINS)
                .setCallBack(this)
                .build();
    }

    void initView() {
        findViewById(R.id.btn_all).setOnClickListener(this);
        findViewById(R.id.btn_year_month_day).setOnClickListener(this);
        findViewById(R.id.btn_year_month).setOnClickListener(this);
        findViewById(R.id.btn_month_day_hour_minute).setOnClickListener(this);
        findViewById(R.id.btn_hour_minute).setOnClickListener(this);

        mTvTime = (TextView) findViewById(R.id.tv_time);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_all:

                long tenYears = 2L * 1000 * 60 * 60 * 24L;
                mDialogAll = new TimePickerDialog.Builder()
                        .setCallBack(this)
                        .setCancelStringId(DefaultConfig.CANCEL)
                        .setSureStringId(DefaultConfig.SURE)
                        .setTitleStringId(DefaultConfig.TITLE)
                        .setYearText(DefaultConfig.YEAR)
                        .setMonthText(DefaultConfig.MONTH)
                        .setDayText(DefaultConfig.DAY)
                        .setHourText(DefaultConfig.HOUR)
                        .setMinuteText(DefaultConfig.MINUTE)
                        .setCyclic(false)
                        .setMinMillseconds(CurrentTimeMillis)
                        .setMaxMillseconds(CurrentTimeMillis + tenYears)
                        .setCurrentMillseconds(millseconds1)
                        .setThemeColor(getResources().getColor(R.color.timepicker_dialog_bg))
                        .setType(Type.ALL)
                        .setWheelItemTextNormalColor(getResources().getColor(R.color.timetimepicker_default_text_color))
                        .setWheelItemTextSelectorColor(getResources().getColor(R.color.timepicker_toolbar_bg))
                        .setWheelItemTextSize(12)
                        .setIsIntervalFiveMinute(true, Interval)
                        .build();

                mDialogAll.show(getSupportFragmentManager(), "all");
                break;
            case R.id.btn_year_month:
                mDialogYearMonth.show(getSupportFragmentManager(), "year_month");
                break;
            case R.id.btn_year_month_day:
                mDialogYearMonthDay.show(getSupportFragmentManager(), "year_month_day");
                break;
            case R.id.btn_month_day_hour_minute:
                mDialogMonthDayHourMinute.show(getSupportFragmentManager(), "month_day_hour_minute");
                break;
            case R.id.btn_hour_minute:
                mDialogHourMinute.show(getSupportFragmentManager(), "hour_minute");
                break;
            default:
                break;
        }
    }


    @Override
    public void onDateSet(TimePickerDialog timePickerDialog, long millseconds) {
        millseconds1 = millseconds;
        String text = getDateToString(millseconds);
        mTvTime.setText(text);
    }

    public String getDateToString(long time) {
        Date d = new Date(time);
        return sf.format(d);
    }

}
